package com.hnd.springframework.beans.factory.support;

import com.hnd.springframework.beans.BeanException;
import com.hnd.springframework.beans.factory.config.BeanDefinition;

import java.util.HashMap;
import java.util.Map;

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
    Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
    static volatile DefaultListableBeanFactory beanFactory = null;

    private DefaultListableBeanFactory() {

    }

    public static DefaultListableBeanFactory getBeanFactory() {
        if (beanFactory == null) {
            synchronized (DefaultListableBeanFactory.class) {
                if (beanFactory == null) {
                    beanFactory = new DefaultListableBeanFactory();
                }
            }
        }
        return beanFactory;
    }

    @Override
    public BeanDefinition getBeanDefinition(String beanName) {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new BeanException("no bean define" + beanName);
        }
        return beanDefinition;
    }

    @Override
    public boolean containsBeanDefinition(String beanName) {
        return false;
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return new String[0];
    }

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        this.beanDefinitionMap.put(beanName, beanDefinition);
    }


}
